package com.guoq.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class DateConvert implements Converter<String, Date> {

	@Override
	public Date convert(String stringDate) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return simpleDateFormat.parse(stringDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * <P>
	 * 开发者:郭强
	 * <P>
	 * 方法名: dateToStr
	 * <P>
	 * 描述: 日期转字符串
	 * 
	 * @param date
	 * @return
	 * @exception @since
	 *                1.0.0
	 */
	public static String dateToStr(Date date) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	/**
	 * 
	 * <P>
	 * 开发者:郭强
	 * <P>
	 * 方法名: strToDate
	 * <P>
	 * 描述: 字符串转日期
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 * @exception @since
	 *                1.0.0
	 */
	public static Date strToDate(String stringDate) throws ParseException {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat.parse(stringDate);
	}

	/**
	 * 
	 * <P>
	 * 开发者:郭强
	 * <P>
	 * 方法名: getTimeByHour
	 * <P>
	 * 描述: 获取当前时间之前或之后几个月
	 * 
	 * @param hour
	 * @return
	 * @exception @since
	 *                1.0.0
	 */
	public static Date getTimeByMonth(int month) {
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + month);
		
		String datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
		try {
			return strToDate(datetime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 
	 * <P>
	 * 开发者:郭强
	 * <P>
	 * 方法名: getTimeByHour
	 * <P>
	 * 描述: 获取当前时间之前或之后几小时 hour
	 * 
	 * @param hour
	 * @return
	 * @exception @since
	 *                1.0.0
	 */
	public static String getTimeByHour(int hour) {

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);

		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

	}

	/**
	 * 
	 * <P>
	 * 开发者:郭强
	 * <P>
	 * 方法名: getTimeByMinute
	 * <P>
	 * 描述: 获取当前时间之前或之后几分钟 minute
	 * 
	 * @param minute
	 * @return
	 * @exception @since
	 *                1.0.0
	 */
	public static Date getTimeByMinute(int minute) {

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, minute);

		String datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
		try {
			return strToDate(datetime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {

		// 获取当前时间5分钟前的时间 格式yyyy-MM-dd HH:mm:ss
		System.out.println(getTimeByMinute(-5));
		// 获取当前时间1小时后的时间 格式yyyy-MM-dd HH:mm:ss
		System.out.println(getTimeByHour(1));

	}
}
